package cn.kent.base.join;

/**
 * 线程A死循环
 * 线程B休眠1s，终止主线程，线程B执行完了，但是线程A还处于死循环阶段，导致抛出异常：java.lang.InterruptedException
 */
public class JoinTest2 {
    public static void main(String[] args) {

        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("threadA begin run!");
                for (; ; ) {
                }
            }
        });

        // 获取主线程
        final Thread mainThread = Thread.currentThread();

        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);// 休眠1s
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 中断主线程
                mainThread.interrupt();
            }
        });

        threadA.start();
        // 延迟1s启动线程
        threadB.start();
        // 等待线程A执行结束 -- 死循环，不会结束的
        try {
            threadA.join();
        } catch (InterruptedException e) {
            System.out.println("main thread:" + e);
        }
    }
}
